<?php
require_once 'entity/Product.php';
require_once 'entity/News.php';
require_once 'entity/ProductDisplay.php';
class Front_Index_Model extends BaseModel{
	public function getTopLeftProduct($context){
		$sql = "SELECT p.id, p.name,p.image,p.price1,p.description,c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productdisplay = '".ProductDisplay::R1LEFT."'
						ORDER BY p.".Product::ORDINAL." ASC, p.".Product::CREATETIME." DESC
				limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getTopRightProduct($context){
		$sql = "SELECT p.id, p.name,p.image,p.price1,p.description,c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productdisplay = '".ProductDisplay::R1RIGHT."'
						ORDER BY p.".Product::ORDINAL." ASC, p.".Product::CREATETIME." DESC
				limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getRow2Product($context){
		$sql = "SELECT p.id, p.name,p.image,p.price1,p.description,c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productdisplay = '".ProductDisplay::ROW2."'
						ORDER BY p.".Product::ORDINAL." ASC, p.".Product::CREATETIME." DESC
				limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getRow3Product($context){
		$sql = "SELECT p.id, p.name,p.image,p.price1,p.description,c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productdisplay = '".ProductDisplay::ROW3."'
						ORDER BY p.".Product::ORDINAL." ASC, p.".Product::CREATETIME." DESC
				limit 0,15";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getTopNews($context){
		$sql = "SELECT n.id, n.name, n.image, c.name as ".News::CATNAME."
				FROM news n
				LEFT JOIN newscat as c ON c.id = n.newscat
				WHERE n.newscat = '1' 
				ORDER BY n.".News::CREATETIME . " DESC 
						LIMIT 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::newsDetails1($context, $value['id'], $value['name'], $value[News::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
}